package com.linzelin.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 用于扩展接管SpringMVC的类
 */
@Configuration
public class MyMVCConfig implements WebMvcConfigurer {
    /**
     * 返回自定义的国际化类
     * @return
     */
    @Bean
    public LocaleResolver localeResolver(){
        return new MyLocalResolver();
    }

    /**
     * 配置拦截器
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //这个方法就是添加拦截器用的了
        registry.addInterceptor(new LoginHandlerInterceptor())
                .addPathPatterns("/**")
                //这个就是配置拦截的基本路径了
                .excludePathPatterns("/index.html","/","/index","/user/login");
                //这个就是配置对应的不想拦截的路径了
    }

    /**
     * 视图跳转解析器
     * @param registry
     */
    @Override
    public void addViewControllers(ViewControllerRegistry registry){
        //这里就可以设置对应的视图跳转了
        registry.addViewController("/lin").setViewName("test");
        //这个就是配置当用户访问/lin这个路径的时候,跳转到test.html下
        //就有点类似controller了
        //不过不用再单独写个方法去配置对应的跳转了
        registry.addViewController("/lxs").setViewName("test");
        registry.addViewController("/").setViewName("index");
        registry.addViewController("/index").setViewName("index");
        registry.addViewController("/login").setViewName("main");
    }
}
